package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "这是套餐的控制类")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询方法
     * @param setmealPageQueryDTO 输入查询套餐条件
     * @return
     */
    @ApiOperation("这里是分页查询方法")
    @GetMapping("/page")
    public Result<PageResult> setmealQueryAllByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("输入参数{}",setmealPageQueryDTO);
        PageResult pageResult = setmealService.setmealQueryAllByPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    @ApiOperation("这里是新增套餐")
    @PostMapping
    public Result insertSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增的数据{}",setmealDTO);
        boolean b = setmealService.insertSetmeal(setmealDTO);
        if (b) {
            return Result.success();
        }else {
            return Result.error("添加失败");
        }
    }

    /**
     * 起售停售套餐
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startAndStopSetmeal(@PathVariable Integer status,Long id){
        boolean b = setmealService.startAndStopSetmeal(status,id);
        if (b) {
            return Result.success();
        }else {
            return Result.error("添加失败");
        }
    }

    /**
     * 修改前的前置条件
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public Result<SetmealVO> selectByid(@PathVariable Long id) {
        SetmealVO setmeal = setmealService.selectById(id);
        return Result.success(setmeal);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        boolean b = setmealService.updateSetmeal(setmealDTO);
        if (b) {
            return Result.success();
        }else {
            return Result.error("添加失败");
        }
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteSetmeal(Long[] ids) {
        boolean b = setmealService.deleteSetmeal(ids);
        if (b) {
            return Result.success();
        }else {
            return Result.error("添加失败");
        }
    }
}
